<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Index extends Base
{
    /**
    *小程序部分
    */
    private function httpRequest($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
  	public function openid(){
        if(!empty(input("param.code"))){
            $code=input("param.code");
        }else{
            return json(['code' => 501, 'msg' => '请给code']);exit();
        }
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=wx897a77a94be25594&secret=da595027c56ce62b942e1332ded3e6aa&js_code=".$code."&grant_type=authorization_code";
        $res=$this->httpRequest($url);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>json_decode($res)]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //轮播图
    public function banner(){
        $res=Db::name('banner')->where('state',1)->order('sort desc')->select();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '暂无数据！']);
        }
    }
    //设施
    public function facilities(){
        $res=Db::name('facilities')->where('state',1)->order('sort desc')->select();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['img']=$this->request->domain().$res[$key]['img'];
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '暂无数据！']);
        }
    }
    //基本信息
    public function systeminfos(){
        $res=Db::name('system_set')->where('id',1)->find();
        if($res){
            $res['latitude']=explode(',', $res['lat_lng'])[0];
            $res['longitude']=explode(',', $res['lat_lng'])[1];
            $res['facilitiesarr']=explode(',', $res['facilities']);
            foreach ($res['facilitiesarr'] as $key => $value) {
                $res['facilitiesarr'][$key]=$this->request->domain().$res['facilitiesarr'][$key];
            }
            $res['count']=count($res['facilitiesarr']);
            $res['introduction'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['introduction']);
            $res['tip'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['tip']);
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '暂无数据！']);
        }
    }
    //可租日期
    public function rent_date(){
        $rent_datef=Db::name('homestay_date')->alias('a')->join('homestay b', 'a.h_id = b.id','LEFT')->where('a.is_rent',1)->where('a.state',1)->where('b.state',1)->where('a.date','>=',strtotime(date('Y-m-d',time())))->order('a.date asc')->value('date');
        $rent_datet=Db::name('homestay_date')->alias('a')->join('homestay b', 'a.h_id = b.id','LEFT')->where('a.is_rent',1)->where('a.state',1)->where('b.state',1)->where('a.date','>=',strtotime(date('Y-m-d',time())))->order('a.date desc')->value('date');
        if($rent_datef&&$rent_datet){
            $date=date('Y-m-d',$rent_datef);//入住显示日期
            $startdate=date('Y-m-d',$rent_datef);//入住开始日期
            $enddate=date('Y-m-d',$rent_datet);//入住开始最后日期

            $tdate=date('Y-m-d',$rent_datef+86400);//离店显示日期
            $tstartdate=date('Y-m-d',$rent_datef+86400);//离店开始日期
            $tenddate=date('Y-m-d',$rent_datet+86400);//离店最后日期
            return json(['code' => 200, 'msg' => '成功！','date'=>$date,'startdate'=>$startdate,'enddate'=>$enddate,'tdate'=>$tdate,'tstartdate'=>$tstartdate,'tenddate'=>$tenddate]);
        }else{
            return json(['code' => 500, 'msg' => '暂无数据！']);
        }
    }
    //民宿
    public function homestay(){
        $res=Db::name('homestay')->where('state',1)->order('sort desc')->select();
        if($res){
            foreach ($res as $k => $v) {
                $res[$k]['imgs']=explode(',', $res[$k]['imgs']);
                $res[$k]['img']=$res[$k]['imgs'][0];
                $is_rent[$k]=Db::name('homestay_date')->where('h_id',$res[$k]['id'])->where('state',1)->where('date','>=',strtotime(date('Y-m-d',time())))->where('date','<',strtotime(date('Y-m-d',time()+86400)))->find();
                if($is_rent[$k]){
                    $res[$k]['today']=$is_rent[$k]['is_rent'];//0:好像未对外开放，1未租;2已租;
                    $res[$k]['price']=$is_rent[$k]['day_price'];
                }else{
                    $res[$k]['today']=0;
                }
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '暂无数据！']);
        }
    }
    //所选日期是否被租
    public function is_rented(){
        if(empty(input("param.id"))){
            return json(['code' => 501, 'msg' => '民宿房间ID']);
        }
        if(empty(input("param.date"))){
            return json(['code' => 502, 'msg' => '入租日期']);
        }
        if(empty(input("param.tdate"))){
            return json(['code' => 503, 'msg' => '离租日期']);
        }
        $is_rented=Db::name('homestay_date')->where('is_rent',2)->where('h_id',input("param.id"))->where('date','>=',strtotime(input("param.date")))->where('date','<',strtotime(input("param.tdate")))->order('id asc')->select();
        //被租
        if($is_rented){
            $is_dated=array();
            foreach ($is_rented as $k => $v) {
                array_push($is_dated,date('Y-m-d',$is_rented[$k]['date']));
            }
            return json(['code' => 201, 'msg' => '已被租日期','data'=>$is_dated]);
        }
        $is_stated=Db::name('homestay_date')->where('state',2)->where('h_id',input("param.id"))->where('date','>=',strtotime(input("param.date")))->where('date','<',strtotime(input("param.tdate")))->order('id asc')->select();
        //未对外开放（禁用）
        if($is_stated){
            $dis_dated=array();
            foreach ($is_stated as $k => $v) {
                array_push($dis_dated,date('Y-m-d',$is_stated[$k]['date']));
            }
            return json(['code' => 201, 'msg' => '未对外开租日期','data'=>$dis_dated]);
        }
        $date=strtotime(input("param.date"));
        $tdate=strtotime(input("param.tdate"));
        $days=($tdate-$date)/86400;
        $notAddarr=array();
        for($i=0;$i<$days;$i++){
            $queryDate=$date+$i*86400;
            $notAdd=Db::name('homestay_date')->where('state',1)->where('is_rent',1)->where('h_id',input("param.id"))->where('date','=',$queryDate)->find();
            if($notAdd){

            }else{
                array_push($notAddarr,date('Y-m-d',$queryDate));
            }
        }
        //未对外开放（后台未添加）
        if(count($notAddarr)>0){
            return json(['code' => 201, 'msg' => '未对外开租日期','data'=>$notAddarr]);
        }
        return json(['code' => 200]);
    }
    //选租日期是否被租
    public function homestay_dates(){
        if(empty(input("param.id"))){
            return json(['code' => 501, 'msg' => '民宿房间ID']);
        }
        if(empty(input("param.date"))){
            return json(['code' => 502, 'msg' => '入租日期']);
        }
        if(empty(input("param.tdate"))){
            return json(['code' => 503, 'msg' => '离租日期']);
        }
        $homestay_dates=Db::name('homestay_date')->where('is_rent',1)->where('state',1)->where('h_id',input("param.id"))->where('date','>=',strtotime(input("param.date")))->where('date','<',strtotime(input("param.tdate")))->order('id asc')->select();
        if($homestay_dates){
            $sum=Db::name('homestay_date')->where('is_rent',1)->where('state',1)->where('h_id',input("param.id"))->where('date','>=',strtotime(input("param.date")))->where('date','<',strtotime(input("param.tdate")))->sum('day_price');
            foreach ($homestay_dates as $k => $v) {
                $homestay_dates[$k]['date']=date('Y-m-d',$homestay_dates[$k]['date']);
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$homestay_dates,'sum'=>$sum]);
        }else{
            return json(['code' => 500, 'msg' => '查询失败！']);
        }
    }
}